package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.MultCouponSetting;
import com.moleq.models.SetFunction;
import com.moleq.util.MoleQUtil;

public class MultCouponSettingService {
	Context mContext;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	
	public MultCouponSettingService(Context context) {
		this.DBHelper = new DatabaseHelper(context);
		this.mContext = context;
	}

	public MultCouponSettingService(String shareDBPath) {
		this.db = SQLiteDatabase.openOrCreateDatabase(shareDBPath, null);
	}
	
	public List<MultCouponSetting> findAll(){
		String sql = "select * from " + DatabaseSchema.MultCouponSetting.SCHEMA_NAME;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		List<MultCouponSetting> Source = new ArrayList<MultCouponSetting>();
		if (cursor == null) {
			return Source;
		}
		while (cursor.moveToNext()) {
			MultCouponSetting item = new MultCouponSetting();
			item.setMc_id(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ID)));
			item.setMc_active(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ACTIVE)));
			item.setMc_cpn_name(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_CPN_NAME)));
			item.setMc_mult(cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_MULT)));
			item.setMc_add_value(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ADD_VALUE)));
			item.setMc_max_value(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_MAX_VALUE)));
			item.setMc_max_total(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_MAX_TOTAL)));
			item.setCreate_datetime(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.CREATE_DATETIME)));
			item.setUpdate_datetime(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.UPDATE_DATETIME)));
			Source.add(item);
		}
		cursor.close();
		return Source;
	}
	
	public MultCouponSetting findMultCouponById(int id){
		String sql = "select * from " + DatabaseSchema.MultCouponSetting.SCHEMA_NAME + " where " + DatabaseSchema.MultCouponSetting.MC_ID + " = " + id;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		MultCouponSetting Source = new MultCouponSetting();
		if (cursor == null) {
			return Source;
		}
		if (cursor.moveToNext()) {
			Source.setMc_id(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ID)));
			Source.setMc_active(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ACTIVE)));
			Source.setMc_cpn_name(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_CPN_NAME)));
			Source.setMc_mult(cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_MULT)));
			Source.setMc_add_value(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ADD_VALUE)));
			Source.setMc_max_value(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_MAX_VALUE)));
			Source.setMc_max_total(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_MAX_TOTAL)));
			Source.setCreate_datetime(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.CREATE_DATETIME)));
			Source.setUpdate_datetime(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.UPDATE_DATETIME)));
		}
		cursor.close();
		return Source;
	}
	
	public MultCouponSetting findActiveMultCoupon(){
		String sql = "select * from " + DatabaseSchema.MultCouponSetting.SCHEMA_NAME + " where " + DatabaseSchema.MultCouponSetting.MC_ACTIVE + " = '1'";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		MultCouponSetting Source = null;
		if (cursor == null) {
			return Source;
		}
		if (cursor.moveToNext()) {
			Source = new MultCouponSetting();
			Source.setMc_id(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ID)));
			Source.setMc_active(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ACTIVE)));
			Source.setMc_cpn_name(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_CPN_NAME)));
			Source.setMc_mult(cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_MULT)));
			Source.setMc_add_value(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ADD_VALUE)));
			Source.setMc_max_value(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_MAX_VALUE)));
			Source.setMc_max_total(cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_MAX_TOTAL)));
			Source.setCreate_datetime(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.CREATE_DATETIME)));
			Source.setUpdate_datetime(cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.MultCouponSetting.UPDATE_DATETIME)));
		}
		cursor.close();
		return Source;
	}
	
	public void update(MultCouponSetting obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"Update " + DatabaseSchema.MultCouponSetting.SCHEMA_NAME
				+ " Set mc_active = ?,mc_cpn_name = ?,mc_mult = ?,mc_add_value = ?,mc_max_value = ?,mc_max_total = ?,update_datetime = strftime('%Y%m%d%H%M%S','now')"
				+ " where mc_id = ?",
				new Object[] {obj.getMc_active(),obj.getMc_cpn_name(),obj.getMc_mult(),obj.getMc_add_value() == 0?null:obj.getMc_add_value()
						,obj.getMc_max_value() == 0?null:obj.getMc_max_value(),obj.getMc_max_total() == 0?null:obj.getMc_max_total(),obj.getMc_id()});
		if(obj.getMc_activeCheckBox()){
			setActive(obj.getMc_id());
		}
	}
	
	public void insert(MultCouponSetting obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("insert into " + DatabaseSchema.MultCouponSetting.SCHEMA_NAME + " values(?,?,?,?,?,?,?,strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))",
				new Object[]{obj.getMc_id(),obj.getMc_active(),obj.getMc_cpn_name(),obj.getMc_mult(),obj.getMc_add_value() == 0?null:obj.getMc_add_value(),obj.getMc_max_value() == 0?null:obj.getMc_max_value(),obj.getMc_max_total() == 0?null:obj.getMc_max_total()});
//		if(obj.getMc_activeCheckBox()){
//			setActive(obj.getMc_id());
//		}
	}
	
	public boolean isExist(String id) {
		String sql = "select * from " + DatabaseSchema.MultCouponSetting.SCHEMA_NAME + " where " +
				DatabaseSchema.MultCouponSetting.MC_ID + " = " + id;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null || !cursor.moveToNext()) {
			cursor.close();
			return false;
		}
		cursor.close();
		return true;
	}
	
	public void save(MultCouponSetting obj) {
		if (isExist(obj.getMc_id())) {
			// update
			update(obj);
		} else {
			// insert
			insert(obj);
		}
	}
	
	public void setActive(String id){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"Update " + DatabaseSchema.MultCouponSetting.SCHEMA_NAME + " Set mc_active = '0' where mc_id <> ?",new Object[]{id});
	}
	
	public void delete(String id){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"Delete from " + DatabaseSchema.MultCouponSetting.SCHEMA_NAME + " where " + DatabaseSchema.MultCouponSetting.MC_ID + " = ?",new Object[]{id});
	}
	
	public List<SetFunction> FindMultNoDesc(){
		List<SetFunction> obj = new ArrayList<SetFunction>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select mc_id,mc_cpn_name from "+ DatabaseSchema.MultCouponSetting.SCHEMA_NAME +" order by mc_id",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		obj.add(0, new SetFunction("0", "Mult Coupon"));
		while(cursor.moveToNext()){
			String mc_id  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_ID));
			String mc_cpn_name  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_CPN_NAME));
			
			obj.add(new SetFunction(mc_id, mc_cpn_name));
		}
		cursor.close();
		return obj;
	}
	
	public String getMultName(String no){
		String obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select mc_cpn_name from "+ DatabaseSchema.MultCouponSetting.SCHEMA_NAME +" where mc_id = "+no,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		if(cursor.moveToNext()){
			obj = cursor.getString(cursor.getColumnIndex(DatabaseSchema.MultCouponSetting.MC_CPN_NAME));
		}
		cursor.close();
		return obj;
	}
}
